﻿Public Class sLEDSetForm


    Private _pi As ProjectItem

    '选择设备的网络地址
    Private _netID As Byte = 0

    '选择的冷库编号
    Private _refID As UShort = 0


    '正在修改参数标志
    Private _isModify As Boolean = False

    Private Sub sLEDSetForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        MyBase.OnClosing(e)
    End Sub

    Private Sub sLEDSetForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _pi = MainForm.pi

        Me.HideAllControl()

        LoadAllRef()


    End Sub


    '隐藏/显示Panel中的所有控件
    Private Sub HideAllControl()
        For Each ctl As Control In Me.ActivePanel.Controls
            ctl.Visible = False
            If ctl.GetType Is GetType(RadioButton) Then
                CType(ctl, RadioButton).Enabled = False
            End If
        Next

        Me.btnModify.Enabled = False
        Me.btnOK.Enabled = False
        Me.lbl_tip.Visible = True
        Me.lblSelected.Text = ""
    End Sub

    Private Sub LoadAllRef()
        Dim tn As TreeNode
        'Dim tn1 As TreeNode

        Dim dts As New DevTypeService(_pi)
        Dim rts As New RefTypeService(_pi)

        For Each dti As DevTypeItem In dts.GeDevGroupList()   '设备列表

            For Each rti As RefTypeItem In rts.GeRefGroupList(dti.NetId) '增加该设备冷库信息

                tn = Me.TreeView1.Nodes.Add(rti.RefName)
                tn.Tag = rti

               
            Next
        Next


        Me.TreeView1.ExpandAll()

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim result As Boolean = False
        Me.Cursor = Cursors.WaitCursor

        TreeView1.Enabled = False

        Me.btnOK.Enabled = False
        Me.btnCancel.Enabled = False
        Me.ActivePanel.Enabled = False

        result = Save()
        Me.btnOK.Enabled = True
        Me.btnCancel.Enabled = True
        Me.ActivePanel.Enabled = True

        Me.Cursor = Cursors.Default

        If result Then
            EnableModify(False)
            msg.Show("参数配置成功")


        Else
            msg.ShowError("保存失败")
        End If

        TreeView1.Enabled = True
    End Sub



    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        Me.btnModify.Enabled = False
        EnableModify(True)


    End Sub

    Private Sub EnableModify(ByVal value As Boolean)

        Me.btnModify.Enabled = Not value
        Me.btnOK.Enabled = value

        For Each ctl As Control In Me.ActivePanel.Controls
            If ctl.GetType Is GetType(RadioButton) Then
                CType(ctl, RadioButton).Enabled = value
            End If
        Next

        Me.nud_lostpowerpredelay.Enabled = value


    End Sub


    ''' <summary>
    ''' 保存所有参数
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Save() As Boolean


        KeyValuePairClass.SetValue(_pi.ProjectID, ("s" + _netID.ToString("000") + _refID.ToString), ("sLED" + (Me.nud_lostpowerpredelay.Value).ToString))

        Return True



        Return True
    End Function

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim tn As TreeNode
        Dim rti As RefTypeItem
        tn = Me.TreeView1.SelectedNode

        If tn Is Nothing Then
            Exit Sub
        End If

        If TypeOf tn.Tag Is DevTypeItem Then
            Exit Sub
        End If

        If TypeOf tn.Tag Is RefTypeItem Then

            TreeView1.Enabled = False

            rti = CType(tn.Tag, RefTypeItem)

            _refID = rti.RefId '记录当前选择的冷库ID
            _netID = rti.NetId   '保存当前选择的设备号


            '显示选择设备的名称
            Me.lblSelected.Text = tn.Text

            '关闭设备选择提示
            lbl_tip.Visible = False





            '禁止点击"修改"按钮前修改参数
            Me.btnModify.Enabled = False
            Me.btnOK.Enabled = False

            For Each ctl As Control In Me.ActivePanel.Controls
                ctl.Visible = True
            Next


            If KeyValuePairClass.GetValue(_pi.ProjectID, ("s" + rti.NetId.ToString("000") + rti.RefId.ToString)) Is String.Empty Then
                Me.nud_lostpowerpredelay.Value = 0
            Else
                Me.nud_lostpowerpredelay.Value = KeyValuePairClass.GetValue(_pi.ProjectID, ("s" + rti.NetId.ToString("000") + rti.RefId.ToString)).Substring(4)
            End If

            EnableModify(False)

           
            Me.btnModify.Enabled = True

        TreeView1.Enabled = True

        End If
    End Sub


End Class